/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.struts.action;

import com.eit.toschoolonline.lms.jdo.VersionDataM;
import com.eit.toschoolonline.lms.service.LmsServiceFactory;
import com.eit.toschoolonline.lms.service.ZipItemService;
import com.eit.toschoolonline.lms.service.spring.FileRepositorySpringBean;
import com.eit.toschoolonline.lms.service.spring.VersionServiceSpringBean;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

/**
 *
 * @author tawatchai
 */
public class DownloadVersionAction extends DownloadAction{



   private LmsServiceFactory lmsServiceFactory;

    protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String vId = request.getParameter("versionId");
        try{
            int versionIdInt = Integer.parseInt(vId);
            
            VersionServiceSpringBean vService = getLmsServiceFactory().getVersionServiceSpringBean();
            ZipItemService zService = getLmsServiceFactory().getZipItemService();
            FileRepositorySpringBean fService = getLmsServiceFactory().getFileRepositorySpringBean();

            VersionDataM version = vService.getVersion(versionIdInt);
            ByteArrayOutputStream bStream = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(bStream);

            File inputFile = new File(fService.getDocumentRootPath(),version.getPath());
            zService.zipDir(inputFile, zos, "");

            Date d = new Date();
            DateFormat dFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String suffix  = dFormat.format(d);
            String contentType = "application/x-zip-compressed";

            response.setContentType("application/x-zip-compressed");
            response.setHeader("Content-disposition", "attachment;filename=update"+suffix+".zip" );
            zos.close();
            byte[] datas = bStream.toByteArray();
            return new ByteArrayStreamInfo(contentType, datas);

        }catch(Exception ex){
            ex.printStackTrace();
        }

//        String sessionId = request.getSession().getId();
//        LearningItemHistoryService historyService = serviceFactory.getLearningItemHistoryService();
//        LearningItemHistoryM history = getService().getLearningItemHistory(refId, sessionId);
//        if (history!=null){
//             int itemId = history.getLearningItem();
//            LearningItemViewM itemView =  getItemService().getItemViewByItemId(itemId);
//            File itemFile = getItemService().getItemFile(itemView);
////            File itemFile = docService.getItemFile(item);
//              if (itemFile.exists()){
//                response.setHeader("Content-disposition", "attachment;filename="+itemView.getFilename());
//                return new FileStreamInfo("application/download", itemFile);
//               };
//        }


//        if (itemFile.exists()){
//            response.setHeader("Content-disposition", "attachment;filename="+item.getFilename());
//            return new FileStreamInfo("application/download", itemFile);
//        }else {
            response.setHeader("Content-disposition", "attachment;filename=fileNotFound.txt");
            String notFoundMessage = "not found item";

            return new ByteArrayStreamInfo("text/txt", notFoundMessage.getBytes());
//        }

    }

    /**
     * @return the lmsServiceFactory
     */
    public LmsServiceFactory getLmsServiceFactory() {
        return lmsServiceFactory;
    }

    /**
     * @param lmsServiceFactory the lmsServiceFactory to set
     */
    public void setLmsServiceFactory(LmsServiceFactory lmsServiceFactory) {
        this.lmsServiceFactory = lmsServiceFactory;
    }
    protected class ByteArrayStreamInfo implements StreamInfo {

        protected String contentType;
        protected byte[] bytes;

        public ByteArrayStreamInfo(String contentType, byte[] bytes) {
            this.contentType = contentType;
            this.bytes = bytes;
        }

        public String getContentType() {
            return contentType;
        }

      //  @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }


    }

}
